Transferring Images with TransparentBlt Correction

The 2nd Demo

by

Janet Terra

Home

SpriteByte

Handling Data

Data Six Pak

Kaliedoscope

Drag and Drop

Simulations w/LB

Design Template

Demos:

Simulated Hyperlink

Slideshow

Space Travel

Corrections:

TransparentBlt Fix

Video Capture Fix


Newsletter help

Index

Newsletter Correction

The article, [Transferring Images with TransparentBlt (Newsletter 128)], was missing the second demo. Thanks to Stefan Pendl for noticing the error. Here is the corrected code printed in its entirety. Please refer back to the original article for discussion of BitBlt, StretchBlt and TransparentBlt. My apologies for the omission. -- Janet

'====================================================================

Demo 2: BitBlt, TransparentBlt and Drawing in Memory

If you haven't done so already, please read [Drawing IN MEMORY (Newsletter 101)] by Alyce Watson . There you'll find all the theory you need to understand that you can transfer an image drawn in memory but you can't transfer an image drawn in a hidden graphic box. This second demo demonstrates loading the cherry.bmp image in memory and then transferring that image to a graphicbox using both BitBlt and TransparentBlt.


'Demo Transferring an Image from a Bitmap Loaded in Memory
'Using  gdi32.dll: BitBlt and msimg32.dll: TransparentBlt
'Copyright Janet Terra December 2004
'This information is free for anyone to use in your program
'but this demo itself may not be copied 'as is' and reposted

'Users of PRE WINDOWS 98, please note:
'The msimg32.dll may not be available for Windows 95
'If running this program gives you - Runtime Error: The
'specified module could not be found. (OS error 16r7E)
'then msimg32.dll is not present on your computer.

    Nomainwin
    WindowWidth = 258
    WindowHeight = 400

'Define 2 Graphicboxes: 1 = BitBlt, 2 = TransparentBlt
    Graphicbox #w.g1, 10, 10, 100, 100
    Statictext #w.st1a, "Image transferred from memory using", 8, 120, 110, 32
    Statictext #w.st1b, "gdi32.dll", 8, 152, 110, 16
    Statictext #w.st1c, "BitBlt", 8, 168, 110, 16
    Graphicbox #w.g2, 140, 10, 100, 100
    Statictext #w.st2a, "Image transferred from memory using", 138, 120, 110, 32
    Statictext #w.st2b, "msimg32.dll", 138, 152, 110, 16
    Statictext #w.st2c, "TransparentBlt", 138, 168, 110, 16


'Load a bitmap.  This demo uses cherry.bmp found in the bmp folder of the
'Liberty BASIC folder.  This demo must be run from the main Liberty BASIC
'program folder in order to find cherry.bmp properly.
    Loadbmp "cherry", DefaultDir$;"\bmp\cherry.bmp"

'Open the Window
    Open "Demo" For Window as #w
    #w, "Trapclose [endProgram]"

'Obtain the handles of the primary window, the source window or graphicbox,
'and the destination windows or graphicboxes.  In this demo, #w.g1 receives the
'image using BitBlt and #w.g2 receives the image using TransparentBlt

    hW = hWnd(#w) 'Window handle
    hWG1 = hWnd(#w.g1) 'Source Handle (#w.g1)
    hWG2 = hWnd(#w.g2) 'Destination Handle (#w.g2)
    hDC = GetDC(hW)
    hDC1 = GetDC(hWG1) 'Source Device Context (#w.g1)
    hDC2 = GetDC(hWG2) 'Destination Device Context (#w.g2)

'Open the msimg32.dll
    Open "msimg32.dll" for DLL as #m

'Create Device Context in Memory
    hDCM = CreateCompatibleDC(0)

'Obtain Handle of the Loaded BMP ("cherry")
    hPic = hBmp("cherry")

'Display Solid Backgrounds
    Call hueBackground "Darkblue", "#w.g1"
    Call hueBackground "Darkblue", "#w.g2"

'Select the Object in Memory
    memPic = SelectObject(hDCM, hPic)

'Draw the  Object in Memory to GraphicBox #w.g1 using BitBlt
    null =BitBlt(hDC1, 0, 0, 88, 65, hDCM, 0, 0, _SRCCOPY)

'Draw the Object in Memory to GraphicBox #w.g2 using TransparentBlt
    null = TransparentBlt(hDC2, 0, 0, 88, 64, hDCM, 0, 0, 88, 64, 16777215)

Wait

[endProgram]
'Release memory from any device contexts created during program
    null = ReleaseDC(hWG1, hDC1)
    null = ReleaseDC(hWG2, hDC2)
    CallDLL #gdi32, "DeleteDC", _
        hDCM as long, _
        null as boolean
'Close msimg32.dll
    Close #m
'Unload bitmap
    Unloadbmp "cherry"
'Close window
    Close #w
'End program
    End


    Sub hueBackground hue$, handle$
        #handle$, "Down; Fill ";hue$
        #handle$, "Flush; Discard"
    End Sub

'====================================================================
'These first 3 Functions obtained from Liberty BASIC Workshop (c) Alyce Watson and
'are used with permission of Alyce Watson
'====================================================================
    Function TransparentBlt(hdcDest, xDest, yDest, wDest, hDest, hdcSource, xSource, ySource, wSource, hSource, clrTransp)
        Calldll #m, "TransparentBlt",_
        hdcDest as long,_        'handle to destination DC
        xDest as long,_   'x-coord of destination upper-left corner
        yDest as long,_   'y-coord of destination upper-left corner
        wDest as long,_     'width of destination rectangle
        hDest as long,_    'height of destination rectangle
        hdcSource as long,_         'handle to source DC
        xSource as long,_    'x-coord of source upper-left corner
        ySource as long,_    'y-coord of source upper-left corner
        wSource as long,_      'width of source rectangle
        hSource as long,_     'height of source rectangle
        clrTransp as ulong,_ 'color to make transparent
        result as boolean
    End Function

    Function GetDC(hwin)
        CallDLL #user32, "GetDC",hwin As long,GetDC as ulong
    End Function

    Function ReleaseDC(hwin, hdc)
        CallDLL #user32, "ReleaseDC",hwin As long,hdc As long,ReleaseDC As long
    End Function

'====================================================================
'These next 3 Functions are derived from information obtained from Liberty BASIC
'Workshop (c) Alyce Watson and are used with permission of Alyce Watson
'====================================================================

    Function CreateCompatibleDC(hDC)
        CallDLL #gdi32, "CreateCompatibleDC", _
            0 as long, _ 'current screen
            CreateCompatibleDC as long 'handle of memory DC
    End Function

    Function SelectObject(hDC,hBmp)
        CallDLL #gdi32, "SelectObject", _
            hDC as long, _ 'Memory Device Context
            hBmp as long, _ 'Handle of Loaded Bitmap
            SelectObject as long 'Returns handle of previous object
    End Function

    Function BitBlt(hdcDest, xDest, yDest, wDest, hDest, hdcSource, xSource, ySource, ROP)
        CallDLL #gdi32, "BitBlt",_
        hdcDest as long,_        'handle to destination DC
        xDest as long,_   'x-coord of destination upper-left corner
        yDest as long,_   'y-coord of destination upper-left corner
        wDest as long,_     'width of destination rectangle
        hDest as long,_    'height of destination rectangle
        hdcSource as long,_         'handle to source DC
        xSource as long,_    'x-coord of source upper-left corner
        ySource as long,_    'y-coord of source upper-left corner
        ROP as ulong,_      'Raster Operation
        result as boolean
    End Function
'====================================================================


Home

SpriteByte

Handling Data

Data Six Pak

Kaliedoscope

Drag and Drop

Simulations w/LB

Design Template

Demos:

Simulated Hyperlink

Slideshow

Space Travel

Corrections:

TransparentBlt Fix

Video Capture Fix


Newsletter help

Index